import 'package:flutter/material.dart';
import 'package:flutter_mall_app/provide/shopping_cart.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import './pages/index_page.dart';
import 'dart:io';
import 'package:flutter/services.dart';
import './provide/child_category_provide.dart';
import 'package:provide/provide.dart';
import './provide/current_Index_provide.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() {
  var providers = Providers();
  providers
    ..provide(Provider<CurrentIndexProvide>.value(CurrentIndexProvide()))
    ..provide(Provider<ShoppingCartProvide>.value(ShoppingCartProvide()))
    ..provide(Provider<ChildCatgoryProvide>.value(ChildCatgoryProvide()));
  runApp(ProviderNode(child: MyApp(), providers: providers));
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  final JPush jpush = new JPush();

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    jpush.getRegistrationID().then((rid) {
      Fluttertoast.showToast(msg: "flutter getRegistrationID: $rid");
    });

    jpush.setup(
      appKey: "431f5838a0b46dd8e2479335",
      channel: "theChannel",
      production: false,
      debug: true,
    );
    jpush.applyPushAuthority(new NotificationSettingsIOS(sound: true, alert: true, badge: true));

    try {
      jpush.addEventHandler(
        onReceiveNotification: (Map<String, dynamic> message) async {
          debugPrint("flutter onReceiveNotification: $message");
        },
        onOpenNotification: (Map<String, dynamic> message) async {
          debugPrint("flutter onOpenNotification: $message");
        },
        onReceiveMessage: (Map<String, dynamic> message) async {
          debugPrint("flutter onReceiveMessage: $message");
        },
      );

    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

//    setState(() {
//      debugLable = platformVersion;
//    });
  }

  @override
  void initState() {
    initPlatformState();
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Container(
      child: MaterialApp(
        title: "商城",
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primaryColor: Colors.red,
          accentColor: Colors.red,
        ),
        home: IndexPage(),
      ),
    );
  }
}
